﻿using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Security.Cryptography.X509Certificates;
using System.Collections.Generic;
using System.Windows.Forms;

namespace BpmConnection2Sec
{
    public class UserTerminalBD
    {
        //uso la naming con l'underscore, stile C, per indicare che gli attributi non saranno
        //esposti con gli accessor method.
        private static TerminalWSService.TerminalWSClient _teStub = null;//new TerminalWSService.TerminalWSClient();
        private static Logger logger = Logger.getInstance();
        //metodo di business
        public static TerminalWSService.UserInfo userInformation(string user, string realmCode)
        {
            TerminalWSService.UserInfo retValue = null;
            try
            {
                _teStub = new TerminalWSService.TerminalWSClient();

                _teStub.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.Root, X509FindType.FindBySubjectName,"Gruppo Bipiemme Root CA");
                
                //                using (OperationContextScope scope = new OperationContextScope(_teStub.InnerChannel))
                //                {
                //                    //SECURITY HEADER: costruisco l'utenza che identifica il servizio
                //                    MessageHeader messageSecurityHeader = new BinarySecurityHeader("IVRFTZ01","IVRCALL","IVRCALL","01");
                //                    //Console.WriteLine("messageSecurityHeader:\n{0}", messageSecurityHeader.ToString());
                //                    MessageHeaders mh = OperationContext.Current.OutgoingMessageHeaders;                    
                //                    mh.Add(messageSecurityHeader);
                retValue = _teStub.getUserInformation(user, realmCode);
                logger.log("Transaction done","STD");
                //                }
            }
            catch (System.TimeoutException timeProblem)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("The service operation timed out. {0} ", timeProblem.Message);
                logger.log("Error while getting user data: " + timeProblem.Message, "ERR");
                _teStub.Abort();
                //System.Windows.Forms.MessageBox.Show("The service operation timed out. "+timeProblem.Message, "APPLICATION ERROR", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                Environment.ExitCode = 1;
                throw new Exception("The service operation timed out. "+timeProblem.Message);
            }
            catch (CommunicationException commProblem)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("There was a communication problem. {0}", commProblem.Message);
                logger.log("Error while getting user data: " + commProblem.Message, "ERR");
                _teStub.Abort();
                //System.Windows.Forms.MessageBox.Show("There was a communication problem. " + commProblem.Message, "APPLICATION ERROR", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                Environment.ExitCode = 1;
                throw new Exception("There was a communication problem. " + commProblem.Message);
            }
            catch (ObjectDisposedException objDisposed)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Unexpected problem. {0}", objDisposed.Message);
                logger.log("Error while getting user data: " + objDisposed.Message, "ERR");
                _teStub.Abort();
                //System.Windows.Forms.MessageBox.Show("Unexpected problem. " + objDisposed.Message, "APPLICATION ERROR", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                Environment.ExitCode = 1;
                throw new Exception("Unexpected problem. " + objDisposed.Message);
            }
            catch(Exception generic){
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Unexpected problem. {0}", generic.Message);
                logger.log("Error while getting user data: " + generic.Message, "ERR");
                _teStub.Abort();
                //System.Windows.Forms.MessageBox.Show("Unexpected problem.. " + generic.Message, "APPLICATION ERROR", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                Environment.ExitCode = 1;
                throw new Exception("Unexpected problem.. " + generic.Message);
            }
            finally
            {
                if (_teStub != null) _teStub.Close();
                logger.log("Transaction closed", "STD");
                Console.ResetColor();
            }
            return retValue;
        }
    }
}
